﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DALFactory;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class DispatchBLL
    {
        private static readonly IBolDA bolDA = DataAccess.CreateBolDA();
        private static readonly IBolDetailDA bolDetailDA = DataAccess.CreateBolDetailDA();
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IDispatchDA dispatchDA = DataAccess.CreateDispatchDA();
        private static readonly IDispatchDetailDA dispatchDetailDA = DataAccess.CreateDispatchDetailDA();
        private static readonly IDriverDA driversDA = DataAccess.CreateDriverDA();
        private static readonly IEquipmentTypeDA equipmentTypeDA = DataAccess.CreateEquipmentTypeDA();
        private PubParasInfo pubParasInfo;
        private static readonly ITransportEquipmentDA transportEquipmentsDA = DataAccess.CreateTransportEquipmentDA();

        public DispatchBLL()
        {
            this.pubParasInfo = null;
        }

        public DispatchBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public DispatchBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> IDS)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                List<DispatchInfo> dispatchInfos = new List<DispatchInfo>();
                DispatchInfo dispatchInfo = new DispatchInfo {
                    DispatchDetailInfos = new List<DispatchDetailInfo>()
                };
                foreach (string id in IDS)
                {
                    dispatchInfo = dispatchDA.GetDispatchByID(dataBase, tran, id, this.dataBaseID[0], this.pubParasInfo);
                    dispatchInfo.DispatchDetailInfos = dispatchDetailDA.GetDispatchDetailListByID(dataBase, tran, id, this.dataBaseID[0]);
                    dispatchInfos.Add(dispatchInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                dispatchDetailDA.DeleteDispatchDetail(dataBase, tran, IDS, this.dataBaseID);
                dispatchDA.DeleteDispatch(dataBase, tran, IDS, this.dataBaseID);
                for (int i = 0; i < IDS.Count; i++)
                {
                    bolDA.DeleteBolByDispatchID(dataBase, tran, IDS[i], this.dataBaseID[0]);
                    bolDetailDA.DeleteBolDetailByDispatchID(dataBase, tran, IDS[i], this.dataBaseID[0]);
                    auditDataBLL.InsertAuditData<DispatchInfo>(dataBase, tran, dispatchInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "DISPATCH", this.dataBaseID[0]);
                    if ((dispatchInfos[i].DispatchDetailInfos != null) && (dispatchInfos[i].DispatchDetailInfos.Count > 0))
                    {
                        for (int j = 0; j < dispatchInfos[i].DispatchDetailInfos.Count; j++)
                        {
                            auditDataBLL.InsertAuditData<DispatchDetailInfo>(dataBase, tran, dispatchInfos[i].DispatchDetailInfos[j], null, AuditAction.Delete, this.pubParasInfo.UserID, "DISPATCH_DETAIL", this.dataBaseID[0]);
                        }
                    }
                    temp = temp + IDS[i] + ",";
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public DataSet GetAllInfo()
        {
            return dispatchDA.GetAllDispatch(this.dataBaseID[0], this.pubParasInfo);
        }

        public DispatchInfo GetInfoByID(string id)
        {
            DispatchInfo dispatchInfo = dispatchDA.GetDispatchByID(id, this.dataBaseID[0], this.pubParasInfo);
            if (dispatchInfo != null)
            {
                dispatchInfo.DispatchDetailInfos = dispatchDetailDA.GetDispatchDetailListByID(id, this.dataBaseID[0]);
            }
            return dispatchInfo;
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            DispatchQueryEntity dispatchQuery = obj as DispatchQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(dispatchQuery.DispatchID))
            {
                sqlWhere.Add(" DISPATCH_ID " + dispatchQuery.DispatchID);
            }
            if (!string.IsNullOrEmpty(dispatchQuery.Status))
            {
                sqlWhere.Add(" STATUS " + dispatchQuery.Status);
            }
            if (!string.IsNullOrEmpty(dispatchQuery.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE " + dispatchQuery.IsActive);
            }
            if (!string.IsNullOrEmpty(dispatchQuery.CreatedBy))
            {
                sqlWhere.Add(" DIS.CREATED_BY " + dispatchQuery.CreatedBy);
            }
            return dispatchDA.GetDispatchByQueryList(sqlWhere, dispatchQuery, this.dataBaseID[0], this.pubParasInfo);
        }

        public string Insert(object obj)
        {
            string dispatchInfoID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                DispatchInfo dispatchInfo = obj as DispatchInfo;
                if (!string.IsNullOrEmpty(dispatchInfo.DispatchID))
                {
                    if (dispatchDA.CheckDispatchIDUnique(dataBase, tran, dispatchInfo.DispatchID, this.dataBaseID[0]))
                    {
                        PublicMethod.GetInstance().ShowAlertMessage("F8200_002", this.pubParasInfo.UserLanguage, new List<string> { dispatchInfo.DispatchID });
                    }
                }
                else
                {
                    dispatchInfo.DispatchID = commonMethod.GenNextNumStr(dataBase, tran, "DISPATCH", this.dataBaseID[0]);
                }
                dispatchInfo.UpdatedBy = dispatchInfo.CreatedBy;
                dispatchInfo.CreatedDate = DateTime.Now;
                dispatchInfo.UpdatedDate = DateTime.Now;
                dispatchDA.InsertDispatch(dataBase, tran, dispatchInfo, this.dataBaseID);
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                auditDataBLL.InsertAuditData<DispatchInfo>(dataBase, tran, null, dispatchInfo, AuditAction.Insert, this.pubParasInfo.UserID, "DISPATCH", this.dataBaseID[0]);
                dispatchInfoID = dispatchInfo.DispatchID;
                if ((dispatchInfo.DispatchDetailInfos != null) && (dispatchInfo.DispatchDetailInfos.Count > 0))
                {
                    for (int i = 0; i < dispatchInfo.DispatchDetailInfos.Count; i++)
                    {
                        dispatchInfo.DispatchDetailInfos[i].DispatchID = dispatchInfoID;
                        if (dispatchDetailDA.CheckDispatchDetailIDUnique(dataBase, tran, dispatchInfo.DispatchDetailInfos[i].DispatchID, dispatchInfo.DispatchDetailInfos[i].LineID.ToString(), this.dataBaseID[0]))
                        {
                            throw new Exception("DispatchID:" + dispatchInfo.DispatchDetailInfos[i].DispatchID + "',LineID:" + dispatchInfo.DispatchDetailInfos[i].LineID.ToString() + "'已经存在! 请重新输入!");
                        }
                        dispatchInfo.DispatchDetailInfos[i].CreatedBy = dispatchInfo.CreatedBy;
                        dispatchInfo.DispatchDetailInfos[i].UpdatedBy = dispatchInfo.UpdatedBy;
                        dispatchInfo.DispatchDetailInfos[i].CreatedDate = DateTime.Now;
                        dispatchInfo.DispatchDetailInfos[i].UpdatedDate = DateTime.Now;
                        dispatchDetailDA.InsertDispatchDetail(dataBase, tran, dispatchInfo.DispatchDetailInfos[i], this.dataBaseID);
                        auditDataBLL.InsertAuditData<DispatchDetailInfo>(dataBase, tran, null, dispatchInfo.DispatchDetailInfos[i], AuditAction.Insert, this.pubParasInfo.UserID, "DISPATCH_DETAIL", this.dataBaseID[0]);
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return dispatchInfoID;
        }

        public string InsertDispatchAndBolDetailList(DispatchInfo dispatchInfo, List<BolDetailInfo> bolDetailInfos)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                int i;
                if (!string.IsNullOrEmpty(dispatchInfo.DispatchID))
                {
                    if (dispatchDA.CheckDispatchIDUnique(dataBase, tran, dispatchInfo.DispatchID, this.dataBaseID[0]))
                    {
                        PublicMethod.GetInstance().ShowAlertMessage("F8200_002", this.pubParasInfo.UserLanguage, new List<string> { dispatchInfo.DispatchID });
                    }
                }
                else
                {
                    dispatchInfo.DispatchID = commonMethod.GenNextNumStr(dataBase, tran, "DISPATCH", this.dataBaseID[0]);
                }
                dispatchInfo.CreatedBy = this.pubParasInfo.UserID;
                dispatchInfo.UpdatedBy = this.pubParasInfo.UserID;
                dispatchInfo.CreatedDate = DateTime.Now;
                dispatchInfo.UpdatedDate = DateTime.Now;
                dispatchDA.InsertDispatch(dataBase, tran, dispatchInfo, this.dataBaseID);
                temp = dispatchInfo.DispatchID;
                if ((dispatchInfo.DispatchDetailInfos != null) && (dispatchInfo.DispatchDetailInfos.Count > 0))
                {
                    IDispatchDetailDA dispatchDetailDA = DataAccess.CreateDispatchDetailDA();
                    for (i = 0; i < dispatchInfo.DispatchDetailInfos.Count; i++)
                    {
                        dispatchInfo.DispatchDetailInfos[i].DispatchID = temp;
                        dispatchInfo.DispatchDetailInfos[i].LineID = (i + 1) * 10;
                        dispatchInfo.DispatchDetailInfos[i].CreatedBy = this.pubParasInfo.UserID;
                        dispatchInfo.DispatchDetailInfos[i].UpdatedBy = this.pubParasInfo.UserID;
                        dispatchInfo.DispatchDetailInfos[i].CreatedDate = DateTime.Now;
                        dispatchInfo.DispatchDetailInfos[i].UpdatedDate = DateTime.Now;
                        dispatchDetailDA.InsertDispatchDetail(dataBase, tran, dispatchInfo.DispatchDetailInfos[i], this.dataBaseID);
                    }
                }
                if ((dispatchInfo.DispatchDetailInfos != null) && (dispatchInfo.DispatchDetailInfos.Count > 0))
                {
                    IDeliveryOrderDA deliveryOrderDA = DataAccess.CreateDeliveryOrderDA();
                    IDeliveryOrderDetailDA deliveryOrderDetailDA = DataAccess.CreateDeliveryOrderDetailDA();
                    for (int j = 0; j < dispatchInfo.DispatchDetailInfos.Count; j++)
                    {
                        DeliveryOrderInfo deliveryOrderInfo = deliveryOrderDA.GetDeliveryOrderByID(dataBase, tran, dispatchInfo.DispatchDetailInfos[j].DeliveryOrderID, this.dataBaseID[0], this.pubParasInfo);
                        if (deliveryOrderInfo != null)
                        {
                            deliveryOrderInfo.Status = DeliveryOrderStatus.LOADED;
                            deliveryOrderInfo.UpdatedBy = this.pubParasInfo.UserID;
                            deliveryOrderDA.UpdateDeliveryOrder(dataBase, tran, deliveryOrderInfo, this.dataBaseID);
                            List<DeliveryOrderDetailInfo> deliveryOrderDetailInfos = deliveryOrderDetailDA.GetDeliveryOrderDetailListByID(dataBase, tran, dispatchInfo.DispatchDetailInfos[j].DeliveryOrderID, this.dataBaseID[0], this.pubParasInfo);
                            if ((deliveryOrderDetailInfos != null) && (deliveryOrderDetailInfos.Count > 0))
                            {
                                for (int a = 0; a < deliveryOrderDetailInfos.Count; a++)
                                {
                                    deliveryOrderDetailInfos[a].Status = DeliveryOrderStatus.LOADED;
                                    deliveryOrderDetailInfos[a].LoadingQty = deliveryOrderDetailInfos[a].OriginalQty;
                                    deliveryOrderDetailInfos[a].UpdatedBy = this.pubParasInfo.UserID;
                                    deliveryOrderDetailDA.UpdateDeliveryOrderDetail(dataBase, tran, deliveryOrderDetailInfos[a], this.dataBaseID);
                                }
                            }
                        }
                    }
                }
                if ((bolDetailInfos != null) && (bolDetailInfos.Count > 0))
                {
                    for (i = 0; i < bolDetailInfos.Count; i++)
                    {
                        if (!string.IsNullOrEmpty(bolDetailInfos[i].ActualEquipment) && !transportEquipmentsDA.CheckTransportEquipmentIDUnique(dataBase, tran, bolDetailInfos[i].ActualEquipment, this.dataBaseID[0]))
                        {
                            throw new Exception("车辆" + bolDetailInfos[i].ActualEquipment + "不存在,请重新输入!");
                        }
                        if (!string.IsNullOrEmpty(bolDetailInfos[i].ActualEquipmentType) && !equipmentTypeDA.CheckEquipmentTypeIDUnique(dataBase, tran, bolDetailInfos[i].ActualEquipmentType, this.dataBaseID[0]))
                        {
                            throw new Exception("车辆类型" + bolDetailInfos[i].ActualEquipmentType + "不存在,请重新输入!");
                        }
                        if (!string.IsNullOrEmpty(bolDetailInfos[i].Driver) && !driversDA.CheckDriverIDUnique(dataBase, tran, bolDetailInfos[i].Driver, this.dataBaseID[0]))
                        {
                            throw new Exception("司机" + bolDetailInfos[i].Driver + "不存在,请重新输入!");
                        }
                        bolDetailInfos[i].DispatchID = temp;
                    }
                }
                new BolDetailBLL(this.pubParasInfo).InsertBolDetailList(dataBase, tran, bolDetailInfos);
                bolDA.UpdateBolTotalByDispatchID(dataBase, tran, temp, this.dataBaseID[0]);
                List<string> DriverIDs = null;
                List<string> TransportEquimentIDs = null;
                if ((bolDetailInfos != null) && (bolDetailInfos.Count > 0))
                {
                    DriverIDs = new List<string>();
                    TransportEquimentIDs = new List<string>();
                    for (i = 0; i < bolDetailInfos.Count; i++)
                    {
                        if (!DriverIDs.Contains(bolDetailInfos[i].Driver))
                        {
                            DriverIDs.Add(bolDetailInfos[i].Driver);
                        }
                        if (!TransportEquimentIDs.Contains(bolDetailInfos[i].ActualEquipment))
                        {
                            TransportEquimentIDs.Add(bolDetailInfos[i].ActualEquipment);
                        }
                    }
                }
                IDriverDA driverDA = DataAccess.CreateDriverDA();
                if ((DriverIDs != null) && (DriverIDs.Count > 0))
                {
                    for (i = 0; i < DriverIDs.Count; i++)
                    {
                        DriverInfo driverInfo = driverDA.GetDriverByID(dataBase, tran, DriverIDs[i], this.dataBaseID[0], this.pubParasInfo);
                        if (driverInfo != null)
                        {
                            driverInfo.Status = DriverStatus.LOADING;
                            driverDA.UpdateDriver(dataBase, tran, driverInfo, this.dataBaseID);
                        }
                    }
                }
                ITransportEquipmentDA transportEquipmentDA = DataAccess.CreateTransportEquipmentDA();
                if ((TransportEquimentIDs != null) && (TransportEquimentIDs.Count > 0))
                {
                    for (i = 0; i < TransportEquimentIDs.Count; i++)
                    {
                        TransportEquipmentInfo transportEquipmentInfo = transportEquipmentDA.GetTransportEquipmentByID(dataBase, tran, TransportEquimentIDs[0], this.dataBaseID[0], this.pubParasInfo);
                        if (transportEquipmentInfo != null)
                        {
                            transportEquipmentInfo.Status = TransportEquipmentStatus.LOADING;
                            transportEquipmentDA.UpdateTransportEquipment(dataBase, tran, transportEquipmentInfo, this.dataBaseID);
                        }
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public string Update(object obj)
        {
            string dispatchInfoID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                DispatchInfo dispatchInfo = obj as DispatchInfo;
                dispatchInfo.UpdatedDate = DateTime.Now;
                DispatchInfo oldDispatchInfo = dispatchDA.GetDispatchByID(dataBase, tran, dispatchInfo.DispatchID, this.dataBaseID[0], this.pubParasInfo);
                if (dispatchDA.UpdateDispatch(dispatchInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<DispatchInfo>(oldDispatchInfo, dispatchInfo, AuditAction.Update, this.pubParasInfo.UserID, "DISPATCH", this.dataBaseID[0]);
                    dispatchInfoID = dispatchInfo.DispatchID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return dispatchInfoID;
        }
    }
}

